﻿using AutoMapper;
using MediatR;
using UserLicense.Applicationes.Command;
using UserModels;
using UserModels.DTO;
using UserRepositorys.Intface;

namespace UserLicense.Applicationes.CommandHandler
{
    public class AccountAddCommandHandler : IRequestHandler<AccountAddCommand, int>
    {
        private readonly IMapper mapper;
        private readonly IAccountRepository accountRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="accountRepository"></param>
        public AccountAddCommandHandler(IMapper mapper, IAccountRepository accountRepository)
        {
            this.mapper = mapper;
            this.accountRepository = accountRepository;
        }

        /// <summary>
        /// 添加账户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(AccountAddCommand request, CancellationToken cancellationToken)
        {
            var account = mapper.Map<AccountRoleAddDTO>(request);
            return await accountRepository.AddAccountRole(account);
        }
    }
}
